#include "CppCallPy.h"

//#include <QString>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <QDebug>

//#include "ceval.h"

static void py_init(void) {
    Py_SetPythonHome(L"D:\\python39");

//    qDebug("py init\r\n");
    Py_Initialize();
    if(!Py_IsInitialized()) {
        return;
    }

    PyRun_SimpleString("import sys");

}

static void py_deinit(void) {
    if(Py_IsInitialized()) {
        Py_Finalize();
    }
}

static const char *append_path_head = "sys.path.append('";
static const char *append_path_tail = "')\0";

int CppCallPy::add_script_path(char* path) {
    if(Py_IsInitialized()) {
//        qDebug() << path << "append_head len" << strlen(append_path_head) <<
//                    "path len" << strlen(path) <<
//                    "tail len" << strlen(append_path_tail) << endl;

        int cmd_len = strlen(append_path_head) + strlen(path) + strlen(append_path_tail) + 1;
        char *cmd = (char *)malloc(cmd_len);
        if(NULL == cmd) {
            return (-2);
        }
        memcpy(cmd, append_path_head, strlen(append_path_head));
        memcpy(&cmd[strlen(append_path_head)], path, strlen(path));
        memcpy(&cmd[strlen(append_path_head) + strlen(path)], append_path_tail, strlen(append_path_tail));
        cmd[cmd_len - 1] = '\0';

//        qDebug() << cmd << endl;

        PyRun_SimpleString(cmd);

        free(cmd);
        cmd = NULL;

        return 0;
    }

    return (-1);
}

PyObject* CppCallPy::import_py_file_name(char *name) {
    if(Py_IsInitialized()) {
        return PyImport_ImportModule(name);
    }
    return NULL;
}

PyObject* CppCallPy::get_py_function(PyObject *py, char *func_name) {
    if(Py_IsInitialized() || !py) {
        return PyObject_GetAttrString(py, func_name);
    }
    return NULL;
}

PyObject* CppCallPy::call_function(PyObject *fu, PyObject *args) {
    if(Py_IsInitialized() || !fu) {
        return PyEval_CallObject(fu, args);
//        PyObject *rt = PyEval_CallObject(fu, args);
//        if(rt) {
//            if(PyList_Check(rt)) {
////                PyList_Size(rt)
//                return PyList_AsTuple(rt);
//            }
//        }

//        return rt;
    }
    return NULL;
}

int CppCallPy::get_list_length(PyObject* l) {
    if(PyList_Check(l)) {
        return PyList_Size(l);
    }
    return (-1);
}

PyObject* CppCallPy::get_list_item(PyObject *l, int index) {
    if(!l) {
        return NULL;
    }
    if(index > get_list_length(l) - 1) {
        return NULL;
    }
    return PyList_GetItem(l, index);
//    return NULL;
}

CppCallPy::CppCallPy() {
    py_init();
}

CppCallPy::~CppCallPy() {
    py_deinit();
}

//void py_init()
//{
//    Py_Initialize();
//    if(!Py_IsInitialized()) {
//        return;
//    }

//    PyRun_SimpleString("import sys");
////    PyRun_SimpleString("sys.path.append('./')");
//    PyRun_SimpleString("sys.path.append('/home/hemingway/workstation/Qt/christopher_fund_monitor/scripts')");


//    PyObject *po = PyImport_ImportModule("get_foundation");
//    if(!po) {
//        printf("Pymodule import failed");
//        return;
//    }

//    PyObject *pf = PyObject_GetAttrString(po, "get_fund_data");
//    if(!pf) {
//        printf("Py function failed");
//        return;
//    }

//    PyObject* args = PyTuple_New(1);
//    QString funCode = "000001";
//    PyTuple_SetItem(args, 0, Py_BuildValue("s", funCode.toStdString().c_str()));

////    PyObject *rt = PyObject_CallFunction(pf, NULL);
//    PyObject *rt = PyEval_CallObject(pf, args);
////    if(PyLong_Check(rt)) {
////        printf("Get return");
////    } else {
////        printf("Null return");
////    }

//    if(PyList_Check(rt)) {
//        printf("Get return");
//        printf("len is %d\r\n", PyList_Size(rt));
//        PyObject *rtt = PyList_AsTuple(rt);
//        PyObject *rtt0 = PyTuple_GetItem(rtt, 0);

//        const char* s = PyUnicode_AsUTF8(rtt0);
//        QString strout = QLatin1String(s);
//        qDebug() << strout << endl;

//    } else {
//        printf("Null return");
//    }

////    Py_Check(rt);
////    if(PyInt_Check)
//    Py_Finalize();
////    PyRun_SimpleStringFlags()
//}
